package com.kylin.common.saas.context;

/**
 * 租户变量控制器
 * @author wuhao
 * @version 1.0 - 2024/2/22
 */
public class TenantContextHolder {

    private TenantContextHolder() {
        throw new IllegalStateException("Utility class");
    }

    private static final ThreadLocal<TenantContext> threadLocal = new ThreadLocal<>();

    /** 设置线程变量 */
    public static void setContext(TenantContext apiContext) {
        threadLocal.set(apiContext);
    }

    /** 获取线程变量 */
    public static TenantContext getContext() {
        TenantContext apiContext = threadLocal.get();
        if(apiContext == null) {
            apiContext = new TenantContext();
        }
        return apiContext;
    }

    /** 移除线程变量 */
    public static void remove() {
        threadLocal.remove();
    }
}
